package com.icss.ah.storeApply.service;

import java.util.HashMap;

import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.icss.ah.base.util.LogUtils;
@Service
public class StoreBindService {

	@Autowired
	private SqlSessionTemplate sqlSessionTemplate;
	/**
	 * 查询店铺认证零售信息
	 * @param query
	 * @return
	 */
	public HashMap queryStoreBindInfo(HashMap query){
		HashMap bindInfo = new HashMap();
		try {
			bindInfo = sqlSessionTemplate.selectOne("StoreBindBean.queryStoreBindInfo",query);
		} catch (Exception e) {
			LogUtils.error(e);
		}
		return bindInfo;
	}
	/**
	 * 零售认证
	 * @param map
	 * @return
	 */
	public String storeBindSave(HashMap map){
		String result = "0, ";
		try {
			//验证烟草零售许可证是否已存在并认证
			HashMap licence = sqlSessionTemplate.selectOne("StoreBindBean.checkLicenceBindExist",map);
			if(licence!=null){
				//烟草零售许可证是否存在
				String clientCount= licence.get("CLIENT_COUNT") == null ? "0" : licence.get("CLIENT_COUNT").toString();
				if(Integer.parseInt(clientCount)==0){
					//烟草零售许可证不存在，验证非烟零售账号密码
					HashMap account = sqlSessionTemplate.selectOne("StoreBindBean.checkAccountBindExist",map);
					if(account!=null){
						//非烟零售账号是否存在
						String accountCount= account.get("ACCOUNT_COUNT") == null ? "0" : account.get("ACCOUNT_COUNT").toString();
						if(Integer.parseInt(accountCount)==0){
							//非烟零售账号不存在
							result = "2,您好，您输入的账号不存在！";
						} else {
							//验证非烟零售密码是否正确
							String password = account.get("PASSWORD") == null ? "" : account.get("PASSWORD").toString();
							String inPassword = map.get("password").toString();
							if(!password.equals(inPassword)){
								result = "2,您好，您输入的账号密码错误！";
							} else {
								//验证此账号是否已被认证
								String shopCount= account.get("SHOP_COUNT") == null ? "0" : account.get("SHOP_COUNT").toString();
								if(Integer.parseInt(shopCount)>0){
									//此账号已被认证
									result = "2,您好，您输入的账号信息已认证！";
								} else {
									//此账号未被认证，执行认证操作
									sqlSessionTemplate.update("StoreBindBean.storeBindSave", map);
									result = "1,认证成功！";
								}
							}
						}
					} else {
						result = "2,您好，您输入的账号不存在！";
					}
				} else {
					//验证此账号是否已被认证
					String shopCount= licence.get("SHOP_COUNT") == null ? "0" : licence.get("SHOP_COUNT").toString();
					if(Integer.parseInt(shopCount)>0){
						//此账号已被认证
						result = "2,您好，您输入的账号信息已认证！";
					} else {
						//此账号未被认证，执行认证操作
						sqlSessionTemplate.update("StoreBindBean.storeBindSave", map);
						result = "1,认证成功！";
					}
				}
			} else {
				result = "2,您好，您输入的许可证号和简码不匹配！";
			}
		} catch (Exception e) {
			e.printStackTrace();
			LogUtils.error(e);
			result = "2,"+e.getMessage();
		}
		return result;
	}
}
